//
//  LZFNavgationController.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/21.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFNavgationController.h"
#import "UIImage+Render.h"

@interface LZFNavgationController ()<UIGestureRecognizerDelegate>

@end

@implementation LZFNavgationController

+(void)load
{
    
    //UINavigationBar 设置导航条
    UINavigationBar *navigationBar=[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
      //UINavigationBar背景颜色
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
      //设置navigationBar的字体大小
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[NSFontAttributeName]=[UIFont systemFontOfSize:20];
    dic[NSForegroundColorAttributeName]=[UIColor blackColor];
    [navigationBar setTitleTextAttributes:dic];
    
    //UIBarButtonItem  设置导航条两边的按钮的样式
//    UIBarButtonItem *barButtonItem=[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
    UIBarButtonItem *barButtonItem=[UIBarButtonItem appearance];
      //normal状态
    NSMutableDictionary *barButtonItemDic=[NSMutableDictionary new];
    barButtonItemDic[NSFontAttributeName]=[UIFont systemFontOfSize:17];
    barButtonItemDic[NSForegroundColorAttributeName]=[UIColor blackColor];
    [barButtonItem setTitleTextAttributes:barButtonItemDic forState:UIControlStateNormal];
    
    //disable状态
    NSMutableDictionary *disabledDic=[NSMutableDictionary new];
    //disabledDic[NSFontAttributeName]=[UIFont systemFontOfSize:30];
    disabledDic[NSForegroundColorAttributeName]=[UIColor grayColor];
    [barButtonItem setTitleTextAttributes:disabledDic forState:UIControlStateDisabled];
    
}

    

-(void)viewDidLoad
{
    //全屏pan返回功能
    //    NSLog(@"%@",self.interactivePopGestureRecognizer);
    //    NSLog(@"%@",self.interactivePopGestureRecognizer.delegate);
    UIPanGestureRecognizer *panGes=[[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    panGes.delegate=self;
    [self.view addGestureRecognizer:panGes];
    
    
    // 清空手势代理,恢复侧滑返回功能
    //只要自定义了返回按钮,系统的侧滑返回的功能就会失效(self.interactivePopGestureRecognizer.delegate干的,所以要想在重写了系统导航返回按钮的话,那么久将self.interactivePopGestureRecognizer.delegate 干掉)
    //self.interactivePopGestureRecognizer.delegate = nil;
    
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    
    //控制只有是非根控制器的时候,才允许该全屏返回手势.
    return self.childViewControllers.count>1;
}


//nav的子控制器入栈时调用
-(void)pushViewController:(UIViewController *)vc animated:(BOOL)animated
{
    NSInteger count =self.childViewControllers.count;
  
    if (count!=0) {
          //非根控制器,设置全局返回按钮
        [self setUpBackButton:vc];
          //push出来的非根控制器,隐藏底部导航条
        vc.hidesBottomBarWhenPushed=YES;
    }
    
    
    [super pushViewController:vc animated:YES];
}


//设置全局返回按钮
-(void)setUpBackButton:(UIViewController *)vc
{
    UIButton *backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    backButton.contentEdgeInsets=UIEdgeInsetsMake(0, -12, 0, 0);
    [backButton sizeToFit];
    
    vc.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backButton];
}



//返回按钮的触发
-(void)clickBackButton
{
    [self popToRootViewControllerAnimated:YES];
}


@end
